<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>深入理解 synchronized 锁的膨胀升级机制</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            position: relative;
            overflow: hidden;
        }
        
        .hero-gradient::before {
            content: '';
            position: absolute;
            top: -50%;
            right: -50%;
            bottom: -50%;
            left: -50%;
            background: radial-gradient(circle, rgba(255,255,255,0.1) 0%, transparent 70%);
            animation: pulse 4s ease-in-out infinite;
        }
        
        @keyframes pulse {
            0%, 100% { transform: scale(1); opacity: 0.5; }
            50% { transform: scale(1.1); opacity: 0.8; }
        }
        
        .content-card {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
            transition: all 0.3s ease;
        }
        
        .content-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 25px 50px rgba(0, 0, 0, 0.15);
        }
        
        .section-title {
            position: relative;
            padding-left: 20px;
        }
        
        .section-title::before {
            content: '';
            position: absolute;
            left: 0;
            top: 50%;
            transform: translateY(-50%);
            width: 4px;
            height: 80%;
            background: linear-gradient(180deg, #667eea 0%, #764ba2 100%);
            border-radius: 2px;
        }
        
        .feature-icon {
            width: 60px;
            height: 60px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 15px;
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
            font-size: 24px;
            margin-bottom: 20px;
            transition: all 0.3s ease;
        }
        
        .feature-icon:hover {
            transform: rotate(10deg) scale(1.1);
        }
        
        .lock-state {
            background: linear-gradient(135deg, #f5f7fa 0%, #e9ecef 100%);
            border-left: 4px solid;
            padding: 20px;
            margin: 15px 0;
            border-radius: 8px;
            transition: all 0.3s ease;
        }
        
        .lock-state:hover {
            transform: translateX(10px);
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
        }
        
        .lock-state.no-lock { border-color: #28a745; }
        .lock-state.biased { border-color: #ffc107; }
        .lock-state.lightweight { border-color: #17a2b8; }
        .lock-state.heavyweight { border-color: #dc3545; }
        
        .code-inline {
            background: linear-gradient(135deg, #f8f9fa 0%, #e9ecef 100%);
            padding: 2px 8px;
            border-radius: 4px;
            font-family: 'Courier New', monospace;
            font-size: 0.9em;
            color: #e83e8c;
            border: 1px solid #dee2e6;
        }
        
        .mermaid {
            background: white;
            padding: 30px;
            border-radius: 12px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
        }
        
        .tip-card {
            background: linear-gradient(135deg, #ffeaa7 0%, #fab1a0 100%);
            padding: 20px;
            border-radius: 12px;
            margin: 20px 0;
            position: relative;
            overflow: hidden;
        }
        
        .tip-card::before {
            content: '\f0eb';
            font-family: 'Font Awesome 6 Free';
            font-weight: 900;
            position: absolute;
            right: 20px;
            top: 50%;
            transform: translateY(-50%);
            font-size: 60px;
            opacity: 0.2;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <div class="hero-gradient text-white py-20 relative">
        <div class="container mx-auto px-6 relative z-10">
            <div class="max-w-4xl mx-auto text-center">
                <h1 class="text-5xl md:text-6xl font-bold mb-6 leading-tight">
                    深入理解 <span class="text-yellow-300">synchronized</span> 锁的膨胀升级机制
                </h1>
                <p class="text-xl md:text-2xl opacity-90 leading-relaxed">
                    探索 Java 虚拟机如何智能地管理锁的升级过程，从无锁到重量级锁的演进之路
                </p>
                <div class="mt-10 flex justify-center space-x-8">
                    <div class="text-center">
                        <i class="fas fa-lock-open text-4xl mb-2"></i>
                        <p class="text-sm">无锁状态</p>
                    </div>
                    <i class="fas fa-arrow-right text-2xl self-center opacity-60"></i>
                    <div class="text-center">
                        <i class="fas fa-user-lock text-4xl mb-2"></i>
                        <p class="text-sm">偏向锁</p>
                    </div>
                    <i class="fas fa-arrow-right text-2xl self-center opacity-60"></i>
                    <div class="text-center">
                        <i class="fas fa-sync text-4xl mb-2"></i>
                        <p class="text-sm">轻量级锁</p>
                    </div>
                    <i class="fas fa-arrow-right text-2xl self-center opacity-60"></i>
                    <div class="text-center">
                        <i class="fas fa-lock text-4xl mb-2"></i>
                        <p class="text-sm">重量级锁</p>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Main Content -->
    <div class="container mx-auto px-6 py-12">
        <div class="max-w-5xl mx-auto">
            
            <!-- Introduction -->
            <div class="content-card rounded-2xl p-8 mb-8">
                <p class="text-lg leading-relaxed text-gray-700 indent-8">
                    <span class="code-inline">synchronized</span> 锁的膨胀升级过程是指Java虚拟机（JVM）在面对不同程度的线程竞争时，逐步提升锁的级别，这个过程涉及从无锁状态到偏向锁、轻量级锁，再到重量级锁的升级。
                </p>
            </div>

            <!-- Lock States Visualization -->
            <div class="content-card rounded-2xl p-8 mb-8">
                <h2 class="text-3xl font-bold mb-8 text-center text-gray-800">锁状态演进图</h2>
                <div class="mermaid">
                    graph LR
                        A[无锁状态] -->|第一个线程访问| B[偏向锁]
                        B -->|其他线程竞争| C[轻量级锁]
                        C -->|竞争激烈/自旋失败| D[重量级锁]
                        
                        style A fill:#28a745,stroke:#fff,stroke-width:2px,color:#fff
                        style B fill:#ffc107,stroke:#fff,stroke-width:2px,color:#fff
                        style C fill:#17a2b8,stroke:#fff,stroke-width:2px,color:#fff
                        style D fill:#dc3545,stroke:#fff,stroke-width:2px,color:#fff
                </div>
            </div>

            <!-- Lock States Details -->
            <div class="grid grid-cols-1 md:grid-cols-2 gap-6 mb-8">
                
                <!-- 无锁状态 -->
                <div class="content-card rounded-2xl p-6">
                    <div class="feature-icon">
                        <i class="fas fa-lock-open"></i>
                    </div>
                    <h3 class="section-title text-2xl font-bold mb-4 text-gray-800">1. 无锁状态</h3>
                    <div class="lock-state no-lock">
                        <h4 class="font-semibold text-lg mb-2 text-gray-700">
                            <i class="fas fa-play-circle mr-2 text-green-500"></i>初始状态
                        </h4>
                        <p class="text-gray-600 leading-relaxed">
                            在同步代码块未被任何线程访问时，对象头中的Mark Word中包含的是对象的默认状态（即无锁状态）。
                        </p>
                    </div>
                    <div class="lock-state no-lock">
                        <h4 class="font-semibold text-lg mb-2 text-gray-700">
                            <i class="fas fa-microchip mr-2 text-green-500"></i>对象头结构
                        </h4>
                        <p class="text-gray-600 leading-relaxed">
                            在64位的JVM中，Mark Word的结构是64位，其中存储了对象的HashCode、GC信息、锁标志位等内容。
                        </p>
                    </div>
                </div>

                <!-- 偏向锁 -->
                <div class="content-card rounded-2xl p-6">
                    <div class="feature-icon" style="background: linear-gradient(135deg, #f39c12 0%, #e67e22 100%);">
                        <i class="fas fa-user-lock"></i>
                    </div>
                    <h3 class="section-title text-2xl font-bold mb-4 text-gray-800">2. 偏向锁</h3>
                    <div class="lock-state biased">
                        <h4 class="font-semibold text-lg mb-2 text-gray-700">
                            <i class="fas fa-key mr-2 text-yellow-500"></i>锁的初次获取
                        </h4>
                        <p class="text-gray-600 leading-relaxed">
                            当一个线程第一次进入同步代码块时，JVM会为该线程设置偏向锁。偏向锁的目的是在无竞争的情况下，减少加锁和解锁的开销。
                        </p>
                    </div>
                    <div class="lock-state biased">
                        <h4 class="font-semibold text-lg mb-2 text-gray-700">
                            <i class="fas fa-exchange-alt mr-2 text-yellow-500"></i>Mark Word的改变
                        </h4>
                        <p class="text-gray-600 leading-relaxed">
                            在对象的Mark Word中，JVM会将该线程的ID记录到Mark Word中，同时将锁标志位改为偏向锁状态。
                        </p>
                    </div>
                    <div class="lock-state biased">
                        <h4 class="font-semibold text-lg mb-2 text-gray-700">
                            <i class="fas fa-star mr-2 text-yellow-500"></i>偏向锁的特性
                        </h4>
                        <p class="text-gray-600 leading-relaxed">
                            偏向锁的好处在于，当持有锁的线程再次进入同步代码块时，不需要执行任何加锁操作即可直接执行代码。
                        </p>
                    </div>
                </div>

                <!-- 轻量级锁 -->
                <div class="content-card rounded-2xl p-6">
                    <div class="feature-icon" style="background: linear-gradient(135deg, #3498db 0%, #2980b9 100%);">
                        <i class="fas fa-sync"></i>
                    </div>
                    <h3 class="section-title text-2xl font-bold mb-4 text-gray-800">3. 轻量级锁</h3>
                    <div class="lock-state lightweight">
                        <h4 class="font-semibold text-lg mb-2 text-gray-700">
                            <i class="fas fa-users mr-2 text-blue-500"></i>锁竞争开始
                        </h4>
                        <p class="text-gray-600 leading-relaxed">
                            如果另一个线程尝试获取已经被偏向锁占有的锁，JVM会首先撤销偏向锁，锁会升级为轻量级锁。此时，JVM会让之前的持有线程停止持有偏向锁。
                        </p>
                    </div>
                    <div class="lock-state lightweight">
                        <h4 class="font-semibold text-lg mb-2 text-gray-700">
                            <i class="fas fa-spinner mr-2 text-blue-500"></i>锁记录与自旋
                        </h4